package com.yimi.policyservice.controller;

import com.github.pagehelper.Page;
import com.yimi.policyservice.common.annotation.Auth;
import com.yimi.policyservice.dto.PageDto;
import com.yimi.policyservice.dto.policyaddr.IdsBean;
import com.yimi.policyservice.dto.product.DisabilitiesProductDto;
import com.yimi.policyservice.dto.product.PolicyItemDto;
import com.yimi.policyservice.dto.product.PolicyProductDto;
import com.yimi.policyservice.dto.product.PolicySuppRuleDto;
import com.yimi.policyservice.entity.base.system.SysUser;
import com.yimi.policyservice.entity.base.system.SysUserDataPermission;
import com.yimi.policyservice.param.product.ProductParam;
import com.yimi.policyservice.service.CaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 方案管理
 */
@RequestMapping("case")
@RestController
public class CaseController  extends  BaseController{

    @Autowired
    private CaseService caseService ;


    /**
     * 创建社保方案
     * @param bean
     * @return
     */
    @RequestMapping(value = "social/product",method = RequestMethod.POST)
    public PolicyProductDto create (@RequestBody PolicyProductDto bean){
        SysUser sysUser = getLoginUser();
        appendUserInfo(bean,sysUser);
        return  caseService.createSocialProduct(bean);
    }



    private void appendUserInfo(PolicyProductDto bean,SysUser sysUser){
        bean.setCreateId(sysUser.getId());
        bean.setCreateName(sysUser.getName());
        bean.setCreateTime(new Date());
        bean.setUpdateId(sysUser.getId());
        bean.setUpdateName(sysUser.getName());
        bean.setUpdateTime(new Date());
    }

    /**
     * 社保方案搜索
     * @param companyId
     * @param addrId
     * @param status
     * @return
     */
    @RequestMapping(value = "socialproduct/search",method = RequestMethod.POST)
    public PageDto searchSocialProduct(@RequestBody ProductParam socialproductParam){
        try {
            handlerPage(socialproductParam);
        } catch (Exception e) {
            e.printStackTrace();
        }
        socialproductParam.setHasDatapermission(true);
        SysUserDataPermission permission = new SysUserDataPermission();
        permission.setUserId(getLoginUser().getId());
        socialproductParam.setUserDataPermission(permission);
        Page<PolicyProductDto> products = caseService.searchSocialProduct(socialproductParam);
//        products.setTotal(100);

        return new PageDto<PolicyProductDto>(products) ;
    }

    /**
     * 删除
     * @param bean
     */
    @RequestMapping(value = "socialproduct/del",method = RequestMethod.POST)
    @Auth
    public void deleteSocialProduct(@RequestBody IdsBean bean){
        caseService.delSocialProduct(bean.getIds());
    }

    @RequestMapping(value = "socialproduct/{id}",method = RequestMethod.GET)
    @Auth
    public PolicyProductDto findOneSocialProduct(@PathVariable String id){
        return  caseService.findOneSocialProduct(id);
    }

    @RequestMapping(value = "socialproduct",method = RequestMethod.PUT)
    @Auth
    public void findOneSocialProduct(@RequestBody PolicyProductDto bean){
          caseService.updateSocialProduct(bean);
    }


    @RequestMapping(value = "socialitem",method = RequestMethod.POST)
    @Auth
    public PolicyItemDto createSocialItem(@RequestBody PolicyItemDto bean){
        SysUser sysUser =  getLoginUser();
        bean.setCreateId(sysUser.getId());
        bean.setCreateTime(new Date());
        bean.setCreateName(sysUser.getName());
        return caseService.createSocialItem(bean);
    }

    @RequestMapping(value = "socialitem",method = RequestMethod.PUT)
    @Auth
    public void updatePolicySocialItem(@RequestBody PolicyItemDto bean){
        caseService.updatePolicySocialItem(bean);
    }

    @RequestMapping(value = "product/codes",method = RequestMethod.GET)
    @Auth
    public List<String> getProductItemCodes( String productId){
        return caseService.getProductCodes(productId);
    }

    @RequestMapping(value = "product/codes/nogroupd",method = RequestMethod.GET)
    @Auth
    public List<String> getProductItemGroupCodes( String productId,String code){
        return caseService.getProductGroupdCodes(productId,code);
    }



    @RequestMapping(value = "socialitembycode",method = RequestMethod.GET)
    @Auth
    public PolicyItemDto findItemByProductIdAndCode(String productId, String code){
        return  caseService.findOneByProductIdAnfCode(productId,code);
    }

    @RequestMapping(value = "deletecode",method = RequestMethod.GET)
    @Auth
    public int deletecode(String productId, String code){
        return  caseService.deletecode(productId,code);
    }

    @RequestMapping(value = "social/rule",method = RequestMethod.POST)
    @Auth
    public PolicySuppRuleDto createRule(@RequestBody PolicySuppRuleDto bean){
        SysUser sysUser = getLoginUser();
        bean.setCreateId(sysUser.getId());
        bean.setCreateTime(new Date());
        bean.setUpdateId(sysUser.getId());
        bean.setUpdateTime(new Date());
        return  caseService.createSocialRule(bean);
    }

    /**
     * 规则搜索
     * @param addrId
     * @param companyId
     * @return
     */
    @RequestMapping(value = "social/rule/search",method = RequestMethod.GET)
    @Auth
    public PageDto searchSocialRule(String addrId,String companyId){
        handlerPage();
        Page<PolicySuppRuleDto>  ruleDtos = caseService.searchSocialRule(addrId,companyId,getLoginUser().getId());
        return new PageDto(ruleDtos);
    }

    /**
     * 规则删除
     * @return
     */
    @RequestMapping(value = "social/rule/del",method = RequestMethod.POST)
    @Auth
    public void searchSocialRule(@RequestBody IdsBean bean){
         caseService.deleteSocialRule(bean.getIds());
    }


    @RequestMapping(value = "social/rule/{id}",method = RequestMethod.GET)
    @Auth
    public PolicySuppRuleDto searchSocialRule(@PathVariable String id){
        return  caseService.findSocialRule(id);
    }

    /**
     * 创建公积金方案
     * @param bean
     * @return
     */
    @Auth
    @RequestMapping(value = "acc/product",method = RequestMethod.POST)
    public PolicyProductDto createAccProduct (@RequestBody PolicyProductDto bean){
        SysUser sysUser = getLoginUser();
        appendUserInfo(bean,sysUser);
        return  caseService.createAccFundProduct(bean);
    }

    /**
     * 社保方案搜索
     * @param companyId
     * @param addrId
     * @param status
     * @return
     */
    @Auth
    @RequestMapping(value = "accfundproduct/search",method = RequestMethod.POST)
    public PageDto searchAccfundProduct(@RequestBody ProductParam accfundproductParam){

        handlerPage(accfundproductParam);
        accfundproductParam.setHasDatapermission(true);
        SysUserDataPermission permission = new SysUserDataPermission();
        permission.setUserId(userId());
        accfundproductParam.setUserDataPermission(permission);
        Page<PolicyProductDto> products = caseService.searchAccfundProduct(accfundproductParam);
        return new PageDto<PolicyProductDto>(products) ;
    }
    @Auth
    @RequestMapping(value = "accfundproduct/{id}",method = RequestMethod.GET)
    public PolicyProductDto findOneAcclProduct(@PathVariable String id){
        return  caseService.findOneAccfundProduct(id);
    }


    @RequestMapping(value = "accfundproduct/codes",method = RequestMethod.GET)
    @Auth
    public List<String> getAccfundProductItemCodes( String productId){
        return caseService.getAccfundProductCodes(productId);
    }



    @RequestMapping(value = "accfunditem",method = RequestMethod.POST)
    @Auth
    public PolicyItemDto createAccFunlItem(@RequestBody PolicyItemDto bean){
        SysUser sysUser =  getLoginUser();
        bean.setCreateId(sysUser.getId());
        bean.setCreateTime(new Date());
        bean.setCreateName(sysUser.getName());
        return caseService.createAccfundItem(bean);
    }

    @RequestMapping(value = "accfunditem",method = RequestMethod.PUT)
    @Auth
    public void updatePolicyAccfundlItem(@RequestBody PolicyItemDto bean){
        caseService.updatePolicyAccfundItem(bean);
    }


    @RequestMapping(value = "accfunditembycode",method = RequestMethod.GET)
    @Auth
    public PolicyItemDto findAccItemByProductIdAndCode(String productId, String code){
        return  caseService.findOneAccfundItemByProductIdAnfCode(productId,code);
    }
    @RequestMapping(value = "deleteAccfunCode",method = RequestMethod.GET)
    @Auth
    public int deleteAccfunCode(String productId, String code){
        return  caseService.deleteAccfunCode(productId,code);
    }



    @Auth
    @RequestMapping(value = "accfundproduct",method = RequestMethod.PUT)
    public void updateAccProduct(@RequestBody PolicyProductDto bean){
        caseService.updateAccProduct(bean);
    }


    /**
     * 删除
     * @param bean
     */
    @RequestMapping(value = "accfundproduct/del",method = RequestMethod.POST)
    @Auth
    public void deleteAccProduct(@RequestBody IdsBean bean){
        caseService.delAccProduct(bean.getIds());
    }





    @RequestMapping(value = "accfund/rule",method = RequestMethod.POST)
    @Auth
    public PolicySuppRuleDto createAccRule(@RequestBody PolicySuppRuleDto bean){
        SysUser sysUser = getLoginUser();
        bean.setCreateId(sysUser.getId());
        bean.setCreateTime(new Date());
        bean.setUpdateId(sysUser.getId());
        bean.setUpdateTime(new Date());
        return  caseService.createAccfundRule(bean);
    }

    /**
     * 规则搜索
     * @param addrId
     * @param companyId
     * @return
     */
    @RequestMapping(value = "accfund/rule/search",method = RequestMethod.GET)
    @Auth
    public PageDto searchAccRule(String addrId,String companyId){
        handlerPage();
        Page<PolicySuppRuleDto>  ruleDtos = caseService.searchAccfundRule(addrId,companyId,userId());
        return new PageDto(ruleDtos);
    }

    /**
     * 规则删除
     * @return
     */
    @RequestMapping(value = "accfund/rule/del",method = RequestMethod.POST)
    @Auth
    public void searchAccRule(@RequestBody IdsBean bean){
        caseService.deleteAccfundRule(bean.getIds());
    }


    @RequestMapping(value = "accfund/rule/{id}",method = RequestMethod.GET)
    @Auth
    public PolicySuppRuleDto getAccRule(@PathVariable String id){
        return  caseService.findAccfundRule(id);
    }

    @RequestMapping(value = "disableproduct",method = RequestMethod.POST)
    @Auth
    public DisabilitiesProductDto createDisabilities(@RequestBody DisabilitiesProductDto bean){
        SysUser sysUser = getLoginUser();
        bean.setCreateId(sysUser.getId());
        bean.setCreateTime(new Date());
        bean.setUpdateId(sysUser.getId());
        bean.setUpdateTime(new Date());
        return  caseService.createDisableProduct(bean);
    }

    @RequestMapping(value = "disableproduct/del",method = RequestMethod.POST)
    @Auth
    public void delDisabilities(@RequestBody IdsBean bean){
          caseService.delDisableProduct(bean.getIds());
    }

    @RequestMapping(value = "disableproduct/search",method = RequestMethod.GET)
    @Auth
    public PageDto searchDisabilities(String companyId,String addrId){
        handlerPage();
        Page<DisabilitiesProductDto> productDtos =  caseService.searchDisableProduct(companyId,addrId,userId());
        return new PageDto<>(productDtos) ;
    }

    @RequestMapping(value = "disableproduct/{id}",method = RequestMethod.GET)
    @Auth
    public DisabilitiesProductDto createDisabilities(@PathVariable String id){
      return  caseService.findOneDisableProduct(id);
    }














}
